﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiseLink.RDS.Model.Common;
using WiseLink.RDS.Model;
using System.Web.Mvc;
namespace WiseLink.RDS.Model
{
    public partial class DiagnosticRepository : RDSRepositoryBase
    {
        public SelectList GetContactResult()
        {
            var query = from c in RDSCommon.ContactResultList
                        select new { Result = c };
            return new SelectList(query.ToList(), "Result", "Result");
        }
        private void UpdateFirstContactTime(Vehicle vehicle, VehicleRepairHistory repairHistory)
        {
            foreach (Fault fault in ccf20Ent.Faults.Where(f => f.IsActive && f.VehicleID == vehicle.VehicleID && f.FirstContactDate == null))
            {
                fault.FirstContactDate = DateTime.Now;
                fault.VehicleRepairHistoryID = repairHistory.VehicleRepairHistoryID;
            }
            foreach (VehicleMaintenanceHistory vmh in ccf20Ent.VehicleMaintenanceHistories.Where(m => m.IsActive && m.VehicleID == vehicle.VehicleID && m.FirstContactDate == null))
            {
                vmh.FirstContactDate = DateTime.Now;
                vmh.VehicleRepairHistoryID = repairHistory.VehicleRepairHistoryID;
            }
        }

        public void AddContactHistoryForVehicle(int vehicleID,string contactResult,string contactRemark)
        {
            Vehicle vehicle = ccf20Ent.Vehicles.Where(v => v.VehicleID == vehicleID).FirstOrDefault();
            ContactHistory ch = new ContactHistory();
            VehicleRepairHistory repairHistory = VehicleRepairStart(vehicle, null);
            ch.IsActive = true;
            ch.VehicleID = vehicleID;
            ch.ContactResult = contactResult;
            ch.Remark = contactRemark;
            ch.VehicleRepairHistoryID = repairHistory.VehicleRepairHistoryID;
            ch.ContactTime = DateTime.Now;
            ccf20Ent.ContactHistories.AddObject(ch);
            UpdateFirstContactTime(vehicle, repairHistory);
            ccf20Ent.SaveChanges();
            if (ch.ContactResult.Contains("拒绝"))
            {
                DeclineCustomer(vehicle, ch.ContactResult);

            }
        }
      
        //客户回场
        public VehicleRepairHistory VehicleRepairStart(Vehicle vehicle, DateTime? inShopDate)
        {
            VehicleRepairHistory repairHistory = ccf20Ent.VehicleRepairHistories.Where(v => v.VehicleID == vehicle.VehicleID && v.IsActive).FirstOrDefault();
            if (repairHistory == null)
            {
                repairHistory = new VehicleRepairHistory();
                repairHistory.VehicleID = vehicle.VehicleID;
                repairHistory.IsActive = true;
                ccf20Ent.VehicleRepairHistories.AddObject(repairHistory);

            }
            if (inShopDate != null)
            {
                repairHistory.InShopDate = inShopDate;
                ccf20Ent.SaveChanges();
            }
            return repairHistory;

        }
        public void DeclineCustomer(Vehicle vehicle, string reason)
        {
            //对于拒绝或放弃的客户，模拟回场与出场
            VehicleRepairHistory vrh = VehicleRepairStart(vehicle, null);
            //对于对于拒绝或放弃的客户，我们需要Reset 与车辆相关的状态，
            //同时虚拟保养项目的处理, X新需求，客户拒绝是不出场，保持现在状态，里程
            //HandleMaintenanceRecord(vehicle, reason);
            //ResetVehicle(vehicle, reason);
            VehicleRepairEnd(vehicle, 0, reason);
        }
        public void VehicleRepairEnd(Vehicle vehicle, int cost, string reason)
        {
            //客户必须先回场，才可以出场
            VehicleRepairHistory repairHistory = ccf20Ent.VehicleRepairHistories.Where(v => v.VehicleID == vehicle.VehicleID && v.IsActive).FirstOrDefault();
            if (repairHistory == null)
            {
                throw new Exception("没有找到客户回场记录，不可以出场");
            }

            repairHistory.OutShopDate = DateTime.Now;
            repairHistory.RepairCost = cost;
            repairHistory.CloseReason = reason;
            repairHistory.IsActive = false;
            //对于客户出场，我们需要Reset 与车辆相关的状态，
            ResetVehicle(vehicle, reason, repairHistory);


        }
        public void ResetVehicle(Vehicle vehicle, string reason, VehicleRepairHistory vrh)
        {

            // reset vehicle table vehiclestatus
            vehicle.IsActive = false;
            vehicle.IsFault = false;
            vehicle.IsMaintenance = false;
            vehicle.IsUrgent = false;
            vehicle.LastContactResult = reason;
            vehicle.LastContactTime = DateTime.Now;
            //vehicle.LatestActiveTime = DateTime.Now;
            vehicle.BookTime = null;
            vehicle.PreviousContactResult = string.Empty;


            // clear faults table
            // clear VehicleMaintenanceHistoryTable
            // clear smshistory
            // clear contacthistory
            // Reset vehicleMaintenanceRecord (LastServiceMileage, LastServiceDate)
            //    *** we shouldn't reset vehicleMaintenanceRecord when 客户出场
            //    *** 客户出场时，我们基于客户所选取的保养项目进行维护项目的重置。


            foreach (UrgentSMSQueue urgentSMS in vehicle.UrgentSMSQueues.Where(c => c.IsActive))
            {
                urgentSMS.IsActive = false;
                urgentSMS.VehicleRepairHistoryID = vrh.VehicleRepairHistoryID;
                urgentSMS.CloseReason = reason;
                urgentSMS.Operator = vehicle.Operator;
                urgentSMS.CloseDate = DateTime.Now;
            }
            foreach (ContactHistory ch in vehicle.ContactHistories.Where(c => c.IsActive))
            {
                ch.IsActive = false;
                ch.CloseDate = DateTime.Now;
                ch.VehicleRepairHistoryID = vrh.VehicleRepairHistoryID;
                ch.CloseReason = reason;
                ch.Operator = vehicle.Operator;
            }
            foreach (SMSHistory ch in vehicle.SMSHistories.Where(c => c.IsActive))
            {
                ch.IsActive = false;
                ch.CloseDate = DateTime.Now;
                ch.VehicleRepairHistoryID = vrh.VehicleRepairHistoryID;
                ch.CloseReason = reason;
                ch.Operator = vehicle.Operator;
            }
            foreach (Fault ch in vehicle.Faults.Where(c => c.IsActive))
            {
                ch.IsActive = false;
                ch.CloseDate = DateTime.Now;
                if (ch.FirstContactDate == null) ch.FirstContactDate = DateTime.Now;
                ch.VehicleRepairHistoryID = vrh.VehicleRepairHistoryID;
                ch.CloseReason = reason;
                ch.Operator = vehicle.Operator;
            }
            foreach (VehicleMaintenanceHistory ch in vehicle.VehicleMaintenanceHistories.Where(c => c.IsActive))
            {
                ch.IsActive = false;
                ch.CloseDate = DateTime.Now;
                ch.CloseReason = reason;
                ch.VehicleRepairHistoryID = vrh.VehicleRepairHistoryID;
                ch.CurrentMileage = vehicle.CurrentMileage;
                ch.Operator = vehicle.Operator;
                if (ch.FirstContactDate == null) ch.FirstContactDate = DateTime.Now;

            }
            if (reason != "客户实际出场")
            {
                foreach (VehicleMaintenanceRecord vmr in vehicle.VehicleMaintenanceRecords)
                {
                    vmr.LastServiceDate = DateTime.Now;
                    vmr.LastServiceMileage = vehicle.CurrentMileage;
                    vmr.Operator = vehicle.Operator;
                }
            }
            ccf20Ent.SaveChanges();
        }
    }
}
